package main

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"time"
	"xorm.io/xorm"
	"xorm.io/xorm/log"
	"xorm.io/xorm/names"
)

type UserInfoTab struct {
	Id                int64     `xorm:"pk comment('id') BIGINT(20)"`
	Username          string    `xorm:"not null comment('用户名') unique VARCHAR(64)"`
	Password          string    `xorm:"not null comment('密码（sha256）') VARCHAR(64)"`
	ProfilePictureUrl string    `xorm:"comment('头像url') VARCHAR(128)"`
	Nickname          string    `xorm:"comment('昵称') VARCHAR(64)"`
	UpdateTime        time.Time `xorm:"not null default 'CURRENT_TIMESTAMP' comment('更新时间') TIMESTAMP"`
	CreateTime        time.Time `xorm:"not null default 'CURRENT_TIMESTAMP' comment('创建时间') TIMESTAMP"`
}

func main() {

	//file := utils.LoadConfigFile("/Users/yuliao.chen/go/src/go_entry_task/entry_core/src/config/dev.ini")

	engine, _ := xorm.NewEngine("mysql", "root:happy15699@tcp(localhost:3306)/entry_task_db?charset=utf8mb4")

	defer engine.Close()

	engine.ShowSQL(true)
	engine.SetLogLevel(log.LOG_DEBUG)
	engine.SetMaxOpenConns(50)
	engine.SetMaxIdleConns(10)

	// 同步表结构到数据库
	//engine.Sync(UserInfo{})
	engine.SetMapper(names.SnakeMapper{})

	//get, _ := engine.Alias("u").Where("u.username = ?", "haha").Get(&UserInfoTab{})
	user := new(UserInfoTab)
	rows, err := engine.Where("id >=?", 1).Rows(user)
	if err != nil {
	}
	defer rows.Close()
	for rows.Next() {
		err = rows.Scan(user)
		fmt.Printf("user=%v\n", user)
		//...
	}

	//session := engine.Table("user_info_tab")
	//query, _ := session.Query("select * from user_info_tab")
	//fmt.Printf("qq= %v \n", get)
}
